package com.hg.concurent.demo5;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: skh
 * @Date: 2020/1/15 16:17
 * @Description:
 */
public class CyclicBarrierDemo2 {

    public static void main(String[] args) throws Exception {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            executorService.submit(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + " step1");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread().getName() + " step2");
                    cyclicBarrier.await();
                    System.out.println(Thread.currentThread().getName() + " step3");
                } catch (Exception e) {
                }
            });
        }

        executorService.shutdown();
    }
}
